package MMSServer;

public class MimeParser {
	
	private String mimdMessage;

	public static final String MIME_VERSION = "MIME-Verson";
	public static final String MIME_CONTENT_TYPE = "Content-Type";
	public static final String MIME_TRANSFER_ENCODE = "Content-Transfer-Encoding";
	public static final String MIME_CONTENT_ID = "Content-Id";
	public static final String MIME_CONENT_DESC = "Content-Description";
	
	private String mimeVersion;
	private String mimeContentType;
	private String mimeTransferEncode;
	private String mimeContentId;
	
	private String mimeContentDesc;
	
	public String getMessageType(){
		return mimeVersion;
	}
	
	public String getTransactionId(){
		return mimeContentType;
	}
	
	public String getVersion(){
		return mimeTransferEncode;
	}
	
	public String getResponseStatus(){
		return mimeContentId;
	}
	
	public String getMessageID(){
		return mimeContentDesc;
	}
	
	public MimeParser(String mimdMessage){
		this.mimdMessage = mimdMessage;
		mimeVersion	= null;
		mimeContentType = null;
		mimeTransferEncode = null;
		mimeContentId = null;
		mimeContentDesc = null;
	}
	
	public void decodeMessage(){
		
		try{
			String[] result = mimdMessage.split("\n");
			
			//for (int x = 0; x<result.length; x++)
		 //System.out.println(result[x]);
			
			for(int i=0; i<result.length; i++)
			{
				if(result[i].startsWith(MIME_VERSION))
					mimeVersion=(result[i].substring((result[i].indexOf(':'))+1));
				
				else if(result[i].startsWith(MIME_CONTENT_TYPE))
					mimeContentType=result[i].substring((result[i].indexOf(':'))+1); 
				
				else if(result[i].startsWith(MIME_TRANSFER_ENCODE))
					mimeTransferEncode=result[i].substring((result[i].indexOf(':'))+1);
				
				else if(result[i].startsWith(MIME_CONTENT_ID))
					mimeContentId=result[i].substring((result[i].indexOf(':'))+1);
				
				else if(result[i].startsWith(MIME_CONENT_DESC))
					mimeContentDesc=result[i].substring((result[i].indexOf(':'))+1);							
			}
			
		}catch (ArrayIndexOutOfBoundsException e){
			System.out.println("Malformed response message");
		}
	}
}
